package com.sctvcloud.dongqu.http;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.google.gson.JsonArray;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.utils.HmacSHA256Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by liuyu on 2017/8/14.
 */

public class ParamsEditor {
    private ArrayMap<String, Object> map;
    private StringBuffer sb;

    public synchronized ParamsEditor put(String key, Object t) {
        if (map == null) {
            map = new ArrayMap<>();
            sb = new StringBuffer();
        }
        map.put(key, t);
        sb.append(t);
        return this;
    }

    public synchronized ParamsEditor put(String key, String... t) {
        if (map == null) {
            map = new ArrayMap<>();

        }
        if (t!=null && t.length>0) {
            sb = new StringBuffer(t[0]);
            for (int i = 1; i < t.length; i++) {
                sb.append(","+t[i]);
            }
            map.put(key, sb.toString());
        }
        return this;
    }

    public synchronized ParamsEditor put(String key, JsonArray ja,String arrayKey) {
        if (map == null) {
            map = new ArrayMap<>();
            sb = new StringBuffer();
        }
        if (ja!=null && ja.size()>0) {
            map.put(key, ja.toString());
            if (!TextUtils.isEmpty(arrayKey)) {
                for (int i = 0; i < ja.size(); i++) {
                    sb.append(ja.get(i).getAsJsonObject().get(arrayKey));
                }
            }else {
                for (int i = 0; i < ja.size(); i++) {
                    sb.append(ja.get(i).getAsString());
                }
            }
        }
        return this;
    }

    public ArrayMap<String, Object> getEncryptedParams(String key, String secretKey) {
        if (!TextUtils.isEmpty(secretKey) && !TextUtils.isEmpty(key) && map != null) {
            ArrayList<String> ls=new ArrayList<>();
            for (int i = 0; i < map.size(); i++) {
                ls.add(map.keyAt(i));
            }
            Collections.sort(ls, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
            });
            sb=new StringBuffer();StringBuffer ssb=new StringBuffer();
            for (int i = 0; i < ls.size(); i++) {
                sb.append(map.get(ls.get(i)));
                ssb.append(ls.get(i)+"  ");
            }
            JLog.e("ssds   "+ssb.toString());

            String en = HmacSHA256Utils.digest(secretKey, sb.toString());
            JLog.e("encrypt  key=" + secretKey + "  content=" + sb.toString() + "  \n" + en);
            map.put(key, en);
        }
        return map;
    }

    public ArrayMap<String, Object> getEncryptedParams(String secretKey) {
        return getEncryptedParams("token", secretKey);
    }

    public String encryptParams(String secret){
        return HmacSHA256Utils.digest(secret, sb.toString());
    }

    public void release() {
        map = null;
        sb = null;
    }
}
